package com.example.demo.controller;

import com.example.demo.entity.ResultEntity;
import com.example.demo.service.serviceimpl.FavoriteServiceImpl;
import com.example.demo.vo.FavoriteVo;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

@RequestMapping("v2/favorite/")
@CrossOrigin
@RestController
public class FavoriteController {
    @Resource
    FavoriteServiceImpl favoriteService;

    // 获取用户收藏列表
    @GetMapping("getUserFavorites")
    private ResultEntity getUserFavorites(@RequestParam("user_id") Integer user_id) {
        ResultEntity resultEntity = new ResultEntity();
        try {
            resultEntity.setCode(0);
            resultEntity.setMsg("获取成功");
            resultEntity.setData(favoriteService.getUserFavorites(user_id));
        } catch (Exception e) {
            resultEntity.setCode(-1);
            resultEntity.setMsg("获取收藏失败");
            e.printStackTrace();
        }
        return resultEntity;
    }

    // 添加收藏（检查是否已存在）
    @PostMapping("addFavorite")
    private ResultEntity addFavorite(@RequestBody FavoriteVo favoriteVo) {
        ResultEntity resultEntity = new ResultEntity();
        try {
            // 检查是否已存在收藏
            if (favoriteService.isFavoriteExists(favoriteVo.getUser_id(), favoriteVo.getProducts_id())) {
                resultEntity.setCode(1);
                resultEntity.setMsg("已添加到收藏");
            } else if (favoriteService.addFavorite(favoriteVo) > 0) {
                resultEntity.setCode(0);
                resultEntity.setMsg("添加收藏成功");
            } else {
                resultEntity.setCode(-1);
                resultEntity.setMsg("添加收藏失败");
            }
        } catch (Exception e) {
            resultEntity.setCode(-1);
            resultEntity.setMsg("错误");
            e.printStackTrace();
        }
        return resultEntity;
    }

    // 根据ID删除收藏
    @DeleteMapping("deleteFavorite")
    private ResultEntity deleteFavorite(@RequestParam("id") Integer id) {
        ResultEntity resultEntity = new ResultEntity();
        try {
            if (favoriteService.deleteFavorite(id) > 0) {
                resultEntity.setCode(0);
                resultEntity.setMsg("删除收藏成功");
            } else {
                resultEntity.setCode(-1);
                resultEntity.setMsg("删除收藏失败");
            }
        } catch (Exception e) {
            resultEntity.setCode(-1);
            resultEntity.setMsg("错误");
            e.printStackTrace();
        }
        return resultEntity;
    }

    // 根据用户ID和商品ID删除收藏
    @DeleteMapping("deleteFavoriteByUserAndProduct")
    private ResultEntity deleteFavoriteByUserAndProduct(@RequestParam("user_id") Integer user_id, @RequestParam("products_id") Integer products_id) {
        ResultEntity resultEntity = new ResultEntity();
        try {
            if (favoriteService.deleteFavoriteByUserAndProduct(user_id, products_id) > 0) {
                resultEntity.setCode(0);
                resultEntity.setMsg("取消收藏成功");
            } else {
                resultEntity.setCode(-1);
                resultEntity.setMsg("取消收藏失败");
            }
        } catch (Exception e) {
            resultEntity.setCode(-1);
            resultEntity.setMsg("错误");
            e.printStackTrace();
        }
        return resultEntity;
    }

    // 检查是否已收藏
    @GetMapping("isFavorite")
    private ResultEntity isFavorite(@RequestParam("user_id") Integer user_id, @RequestParam("products_id") Integer products_id) {
        ResultEntity resultEntity = new ResultEntity();
        try {
            boolean exists = favoriteService.isFavoriteExists(user_id, products_id);
            resultEntity.setCode(0);
            resultEntity.setMsg("查询成功");
            resultEntity.setData(exists);
        } catch (Exception e) {
            resultEntity.setCode(-1);
            resultEntity.setMsg("查询失败");
            e.printStackTrace();
        }
        return resultEntity;
    }
}